﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MeTalker.Model;
using MeTalker.BLL;
using MeTalker.Securitys.Cryptography;
using System.Text;
using System.Data;
public partial class RegistrPage : PageBase
{
    string info;
    string id; string mssg1;
    //protected int GetDay(DateTime nowTime, DateTime endTime)
    //{
    //    TimeSpan span = nowTime.Subtract(endTime);
    //    //return span.Days + 1;
    //    return span.Days;
    //}
    protected void Page_Load(object sender, EventArgs e)
    {
        //int d = DateTime.Compare(DateTime.Now, DateTime.Parse("2011-10-09 09:31:26"));
      //int d =  GetDay(DateTime.Now, DateTime.Parse("2011-10-08 09:31:26"));
      //Response.Write(d);
        //if (!this.Page.IsPostBack)
        //{
        //    ddlSoft.DataTextField = "SoftName";
        //    ddlSoft.DataValueField = "SoftID";
        //    ddlSoft.DataSource = SoftwaresBLL.GetSoftwares();
        //    ddlSoft.DataBind();
        //}
        info = Request.QueryString["if"];
        id = Request.QueryString["soft"];
        // if (!string.IsNullOrEmpty(id))
        // {
        //     int index = 0;
        //     foreach (ListItem d in ddlSoft.Items)
        //     {
        //         if (id.Equals(d.Value))
        //         {
        //             ddlSoft.SelectedIndex = index;
        //         }
        //         index++;
        //     }
        // }
        //Response.Write(info);
         //if (!string.IsNullOrEmpty(info))
         //{
         //    byte[] dd  = HexToByte(info);
         //    AESHelper aes = new AESHelper();
         //    mssg1 = Encoding.UTF8.GetString(aes.Decrypt(dd));
         //    Console.WriteLine(mssg1);
         //}
    }


    
    protected void btnReg_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) {
            return;
        }
        if (string.IsNullOrEmpty(txtUserName.Text))
        {
            lblUseName.Text = "*用户名不能为空!";
            return;
        }
        else {
            lblUseName.Text = "*";
        }
        if (string.IsNullOrEmpty(txtPass1.Text))
        {
            lblPass1.Text = "*密码不能为空!";
            return;
        }
        else {
            lblPass1.Text = "*";
        }
        if (!txtPass1.Text.Equals(txtPass2.Text))
        {
            lblPass2.Text = "*输入两次密码不一致!";
            return;
        }
        else {
            lblPass2.Text = "*";
        }
        //if (string.IsNullOrEmpty(txtEmail.Text))
        //{
        //    lblEmail.Text = "*邮箱不能为空!";
        //    return;
        //}else
        //{
        //    lblEmail.Text = "*";
        //}
        //if (string.IsNullOrEmpty(txtTelPhone.Text))
        //{
        //    lblTel.Text = "*手机号码不能为空!";
        //    return;
        //}
        //else {
        //    lblTel.Text = "*";
        //}
        int errcode = 0;
        if (MembersBLL.CheckUserName(txtUserName.Text, out errcode))
        {
            lblUseName.Text = "*该用户已经注册";
            return;
        }
        else {
            lblUseName.Text = "";
        }
        string hdid = null;
        if (!string.IsNullOrEmpty(info))
        {
            try
            {
                byte[] b = HexToByte(info);
                AESHelper aes = new AESHelper();
                mssg1 = Encoding.UTF8.GetString(aes.Decrypt(b));
                hdid = mssg1.Split(',')[0].Split('=')[1];
            }
            catch {
                lblState.Text = "注册失败! ";
            }
        }

        SoftwaresMod software = SoftwaresBLL.GetSoftware(id);
        MembersMod membes = new MembersMod();
        membes.RegistDate = DateTime.Now;
        membes.ExpireDate = DateTime.Now.AddDays(software.TrialDays);
        membes.LoginFlag = false;
        membes.IsValid = true;
        membes.Memo = "";
        membes.SessionId = Session.SessionID;
        membes.Email = txtEmail.Text;
        membes.Password = txtPass1.Text;
        membes.QQ = txtQQ.Text;
        membes.AddDate = membes.RegistDate;
        membes.UserName = txtUserName.Text;
        membes.MobilePhone = txtTelPhone.Text;
        membes.UserType = "0";
        membes.SoftID = software.SoftID;
        membes.HDID = hdid;
        membes.SoftName = software.SoftName;
        DataTable dt = MembersBLL.GetUsers(membes);
        if (dt != null && dt.Rows.Count > 0) {
           // Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert(' ');</script>");
            lblState.Text = "该用户已经注册!";
            return;
        }
        if (MembersBLL.Insert(membes) > 0)
        {
            btnReg.Enabled = false;
            lblState.Text = "用户注册成功!";
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('用户注册成功! ');</script>");

        }
        else {
            lblState.Text = "用户注册失败!";
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('用户注册失败! ');</script>");
        }

    }
    protected void txtUserName_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtUserName.Text))
        {
            int errcode = 0;
            if (MembersBLL.CheckUserName(txtUserName.Text, out errcode))
            {
                lblUseName.Text = "该用户已经注册";
            }
            else { lblUseName.Text = ""; }
        }
    }
    protected void txtEmail_TextChanged(object sender, EventArgs e)
    {
        //if (!string.IsNullOrEmpty(txtEmail.Text))
        //{
        //    if (MembersBLL.CheckUserName(txtEmail.Text))
        //    {
        //        lblEmail.Text = "该邮箱已经注册";
        //    }
        //    else { lblEmail.Text = ""; }
        //}
    }
}